//
//  PersonalMessageViewController.m
//  JiongTu
//
//  Created by Dupengfei on 16/3/22.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "PersonalMessageViewController.h"
#import "PersonaleTableViewCell.h"

#import "CertificationViewController.h"
#import "UIImageView+WebCache.h"
#import "MyMoneyViewController.h"
#import "CollectionViewController.h"
#import "JTUser.h"
#import "CarInfoViewController.h"

#import "PersonalInfoEditController.h"

#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"

#import <SVProgressHUD.h>
#import <AVOSCloud.h>

@interface PersonalMessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIBarButtonItem * rightButton;

@property (weak, nonatomic) IBOutlet UIImageView *bankgrundImage;
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *telphoneNumber;
@property (weak, nonatomic) IBOutlet UILabel *nickName;

@property (weak, nonatomic) IBOutlet UILabel *genderLabel;

@property (nonatomic, strong)NSMutableDictionary * dic;
@property (nonatomic, assign)BOOL  audit;
@property (nonatomic, strong)JTUser *currentUser;

@end

@implementation PersonalMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeInterface];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self initializeDateSource];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self initializeDateSource];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    _headImage.layer.masksToBounds = YES;
    _headImage.layer.cornerRadius = 54;
    _headImage.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    _headImage.layer.borderWidth = 1;
}
//设置图像显示控件为圆形
//- (void)changeToCirclePicture {
//    //设置圆角半径为方形边长一半
//    [self.imageView.layer setCornerRadius:CGRectGetHeight([self.imageView bounds]) / 2];
//    [self.imageView.layer setMasksToBounds:YES];
//    //设置边框宽度和颜色
//    [self.imageView.layer setBorderWidth:10];
//    [self.imageView.layer setBorderColor:[[UIColor grayColor] CGColor]];
//}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
}

-(void)initializeInterface
{
    self.navigationItem.rightBarButtonItem = self.rightButton;
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.nickName.userInteractionEnabled = NO;
    
    self.title = @"个人信息";
}

-(void)initializeDateSource
{
    self.currentUser = [JTUser currentUser];
    [self.headImage sd_setImageWithURL:self.currentUser.iconPath placeholderImage:[UIImage imageNamed:@"头像占位"]];
    [self.bankgrundImage sd_setImageWithURL:self.currentUser.iconPath placeholderImage:[UIImage imageNamed:@"头像占位"]];
    
    self.nickName.text = _currentUser.nickname;
    self.genderLabel.text = self.currentUser.gender;
    self.telphoneNumber.text = _currentUser.mobilePhoneNumber;
}

#pragma mark-delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     PersonaleTableViewCell*cell = [[[NSBundle mainBundle] loadNibNamed:@"PersonaleTableViewCell" owner:nil options:nil] lastObject];
    switch (indexPath.row) {
        case 0:
            cell.nameLabel.text = @"我的收藏";

            cell.image.image = [UIImage imageNamed:@"个人信息_10"];
            break;
        case 1:
            cell.nameLabel.text = @"我的钱包";
            cell.image.image = [UIImage imageNamed:@"个人信息_14"];

            break;
        case 2:
        {
            cell.nameLabel.text = @"实名认证";
            

            
            if ([_currentUser.audit intValue] ==0) {
              
                cell.buttonImage.image = [UIImage imageNamed:@"个人信息_25"];

            }
            else
            {
                cell.buttonImage.image =[UIImage imageNamed:@"个人信息_18"];
            }
            
            cell.image.image = [UIImage imageNamed:@"个人信息_21"];

            
        }
            
            break;
        case 3:
        
            
            if ([_currentUser.carAudit intValue] ==0) {
                
                cell.buttonImage.image = [UIImage imageNamed:@"个人信息_25"];
                
            }
            else
            {
                cell.buttonImage.image =[UIImage imageNamed:@"个人信息_18"];
            }

            cell.nameLabel.text = @"实车认证";
            
            cell.image.image = [UIImage imageNamed:@"个人信息_27"];

            break;
            
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row) {
        case 0:
        {
            CollectionViewController * VC = [[CollectionViewController alloc]init];
            
            [self.navigationController pushViewController:VC animated:YES];
   
        }
            break;
        case 1:
        {
            MyMoneyViewController * view = [[MyMoneyViewController alloc]init];
                    [self.navigationController pushViewController:view animated:YES];
        }
            break;
        case 2:
        {
            if ([_currentUser.audit intValue]==0) {
                CertificationViewController * VC = [[CertificationViewController alloc]init];
                
                [self.navigationController pushViewController:VC animated:YES];

            }
            
        }
            break;
        case 3:
        {
            if ([_currentUser.audit intValue]==0) {
                [SVProgressHUD showErrorWithStatus:@"请先进行实名认证"];
            } else {
                if ([_currentUser.carAudit intValue]==0) {
                    
                    CarInfoViewController * VC = [[CarInfoViewController alloc]init];
                    
                    [self.navigationController pushViewController:VC animated:YES];
                    
                }
            }
                   }
            break;
            
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

#pragma mark -getter
-(UIBarButtonItem *)rightButton
{
    if (!_rightButton) {
        
        _rightButton =[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(RightButtonPressed:)];
        
        _rightButton.tag =1010;
        
    }
    return _rightButton;
}


#pragma mark-交互事件

-(void)RightButtonPressed:(UIBarButtonItem *)sender
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PersonalInfoEditController *vc = [sb instantiateViewControllerWithIdentifier:@"editInfo"];
    [self.navigationController pushViewController:vc animated:YES];
    
}

@end
